package com.wanke.dao.impl;
// Generated 2016-12-1 14:23:46 by Hibernate Tools 4.0.1.Final

import java.util.List;

import javax.annotation.Resource;
import javax.naming.InitialContext;
import javax.xml.namespace.QName;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.springframework.stereotype.Repository;

import com.wanke.dao.BanjiDao;
import com.wanke.entity.Banji;
import com.wanke.entity.Teacher;

/**
 * Home object for domain model class Banji.
 * @see com.wanke.entity.Banji
 * @author Hibernate Tools
 */
@Repository("banjiDao")
public class BanjiDaoImpl implements BanjiDao {

	private static final Log log = LogFactory.getLog(BanjiDaoImpl.class);

	@Resource(name="sessionFactory")
	 SessionFactory sessionFactory ;


	/* (non-Javadoc)
	 * @see com.wanke.dao.BanjiDao#persist(com.wanke.entity.Banji)
	 */
	@Override
	public void persist(Banji transientInstance) {
		log.debug("persisting Banji instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.BanjiDao#attachDirty(com.wanke.entity.Banji)
	 */
	@Override
	public void attachDirty(Banji instance) {
		log.debug("attaching dirty Banji instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.BanjiDao#attachClean(com.wanke.entity.Banji)
	 */
	@Override
	public void attachClean(Banji instance) {
		log.debug("attaching clean Banji instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.BanjiDao#delete(com.wanke.entity.Banji)
	 */
	@Override
	public void delete(Banji persistentInstance) {
		log.debug("deleting Banji instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.BanjiDao#merge(com.wanke.entity.Banji)
	 */
	@Override
	public Banji merge(Banji detachedInstance) {
		log.debug("merging Banji instance");
		try {
			Banji result = (Banji) sessionFactory.getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.BanjiDao#findById(int)
	 */
	@Override
	public Banji findById(int id) {
		log.debug("getting Banji instance with id: " + id);
		try {
			Banji instance = (Banji) sessionFactory.getCurrentSession().get("com.wanke.entity.Banji", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.BanjiDao#findByExample(com.wanke.entity.Banji)
	 */
	@Override
	public List findByExample(Banji instance) {
		log.debug("finding Banji instance by example");
		try {
			List results = sessionFactory.getCurrentSession().createCriteria("com.wanke.entity.Banji")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	@Override
	public Banji findByName(String name) {
		String hql="from Banji as banji where banji.name=:name";
		Banji banji= (Banji) sessionFactory.getCurrentSession().createQuery(hql).setParameter("name", name).list().get(0);
		return banji;
	}

	@Override
	public Banji findByNameAndTeacher(String name, Teacher teacher) {
		String hql="from Banji as banji where banji.name=:name and banji.teacher=:teacher";
		
		return (Banji) sessionFactory.getCurrentSession().createQuery(hql).list().get(0);
	}
	
	public List<Banji> getBjByTeachId(int id){
		try{
			String hql="from Banji bj where bj.teacher.id=?";
			Query query=sessionFactory.getCurrentSession().createQuery(hql);
			query.setInteger(0, id);
			return query.list();
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
}
